package com.douban.business;

import java.util.Collection;
import java.util.LinkedList;

import junit.framework.TestCase;

import com.core.ContentTestFetcher;
import com.core.UserTestRequest;
import com.douban.constants.DoubanMediaTypeConstant;
import com.douban.constants.DoubanUserActionConstant;

public class DoubanWorkerTest extends TestCase {

	DoubanWorker unitundertest;
	
	public void testTaskThread() throws Exception{
		
		Collection ur1 = new LinkedList();
		ur1.add("1");
		ur1.add("2");
		
		Collection ur2 = new LinkedList();
		ur2.add("1");
		
		Collection ur3 = new LinkedList();
		ur3.add("2");
		
		UserTestRequest mainRequests = new UserTestRequest("username", ur1);
		UserTestRequest brokendownRequest1 = new UserTestRequest("username", ur2);
		UserTestRequest brokendownRequest2 = new UserTestRequest("username", ur3);
		
		ContentTestFetcher fetcher1 = new ContentTestFetcher(brokendownRequest1);
		ContentTestFetcher fetcher2 = new ContentTestFetcher(brokendownRequest2);
		
		DoubanWorker.getInstance().addRequestIntoRequestMap(mainRequests);
		DoubanWorker.getInstance().addFetcherIntoWorkQueue(fetcher1);
		DoubanWorker.getInstance().addFetcherIntoWorkQueue(fetcher2);

		assertEquals(1, DoubanWorker.getInstance().getOngoingUsers().size());
		assertEquals(2, DoubanWorker.getInstance().getWorkQueue().size());
		
		DoubanWorker.getInstance().doWork();
		
		Thread.sleep(1000);
		
		assertEquals(0, DoubanWorker.getInstance().getOngoingUsers().size());
		assertEquals(0, DoubanWorker.getInstance().getWorkQueue().size());
		
		DoubanWorker.getInstance().reset();
		
		
	}
	
	public void testBreakDownWork(){
		
		DoubanMediaTypeConstant c = DoubanMediaTypeConstant.BOOK;
		DoubanUserActionConstant d = DoubanUserActionConstant.COLLECT;
		
		DoubanWorker.getInstance("fakeproperty");
		
		DoubanWorker.getInstance().addUserRequest("username1",new DoubanMediaTypeConstant[]{c}, new DoubanUserActionConstant[]{d});
		DoubanWorker.getInstance().breakDownWork();
		
		assertEquals(5,  DoubanWorker.getInstance().getMaxUser());
		assertEquals(1,  DoubanWorker.getInstance().getOngoingUsers().size());
		assertEquals(1,  DoubanWorker.getInstance().getWorkQueue().size());
		
		DoubanWorker.getInstance().addUserRequest("username2");
		DoubanWorker.getInstance().addUserRequest("username1");
		
		assertEquals(5,  DoubanWorker.getInstance().getMaxUser());
		assertEquals(1,  DoubanWorker.getInstance().getOngoingUsers().size());
		assertEquals(1,  DoubanWorker.getInstance().getWorkQueue().size());
		
		DoubanWorker.getInstance().breakDownWork();
		
		assertEquals(5,  DoubanWorker.getInstance().getMaxUser());
		assertEquals(2,  DoubanWorker.getInstance().getOngoingUsers().size());
		assertEquals(10,  DoubanWorker.getInstance().getWorkQueue().size());
		
		DoubanWorker.getInstance().addUserRequest("username3");
		DoubanWorker.getInstance().addUserRequest("username4");
		DoubanWorker.getInstance().addUserRequest("username5");
		DoubanWorker.getInstance().addUserRequest("username6");
		
		DoubanWorker.getInstance().breakDownWork();
		
		assertEquals(5,  DoubanWorker.getInstance().getMaxUser());
		assertEquals(5,  DoubanWorker.getInstance().getOngoingUsers().size());
		assertEquals(37,  DoubanWorker.getInstance().getWorkQueue().size());
		
	}
}
